<?php

/**
 * @author	barbarosalcin
 * @desc	page_admin_userlogs
 * @version	$Id: 20110317
 * @package	admin
 */

if ($tk->session->data['user_level'] != ADMIN)
{
	$allowed = 0;
	return;
}

$error = 0;
$filename = basename(__FILE__);
$pagename = substr(substr($filename, 0, - 4), 5, 99);
define('IN_ADMINPAGE', true);

$item_table = USERS_LOG_TABLE;

/**
 * @ignore createtime,status
 */

$item_fields = "time,user_id,ip";

/**
 * @uses text/imagebrowser/textarea/ckeditor/hidden/file/select/date/checkbox/number/readonly_text/file_upload
 */

$sql = "SELECT SQL_CACHE user_id,username,user_firstname,user_surname from " . USERS_TABLE . " where user_id > 0 order by username asc";

if (! ($result = $tk->db->sql_query($sql)))
{
	$error = 'sql';
	goto end;
}

while($row = $tk->db->sql_fetchrow($result))
{
	$tmp[$row['user_id']] = $row['user_firstname'] . ' ' . $row['user_surname'] . ' (' . $row['username'] . ')';
}

$tk->_page->pv['users'] = $tmp;
unset($tmp);

/* default ! do not edit */

$item_fields_additem = preg_replace('/(\w+)/i', '\'\$$1\'', $item_fields);
$item_fields_manitem = preg_replace('/(\w+)/i', '$1=\'\$$1\'', $item_fields);

if ((int) $_POST['submit'])
{
	include ('pages/admin/ajax_' . $pagename . '.php');
	return;
}

/* get list */

if (! empty($setmodules))
{
	/*$modules[$pagename]['additem'] = $req['language'] . '-' . $pagename . $config['seoext'] . '?act=additem';*/
	$modules[$pagename]['manitem'] = $req['language'] . '-' . $pagename . $config['seoext'] . '?act=manitem';
	return;
}

if ($req['get']['act'] == 'additem')
{
	
	/* default ! do not edit */
	$tk->page->_pv['items'] = $item_array;
	$tk->page->_pv['data']['act'] = $req['get']['act'];
	$tk->page->_pv['data'][$req['get']['act']] = $req['language'] . '-' . $pagename . '.html?';
}

if ($req['get']['act'] == 'manitem')
{
	/**
	 * @uses text,select,time 
	 */
	$search_array = array(
			'id_only' => array(
					'user_id' => 'select'), 
			'ip_only' => array(
					'ip' => 'text'), 
			'time_period' => array(
					'starttime' => 'time', 
					'endtime' => 'time'), 
			'arr' => array());
	
	$search_array['arr']['user_id'] = $tk->_page->pv['users'];
	
	if ((int) $req['get']['s'])
	{
		$search_type = $req['get']['f'];
		
		foreach ( $search_array[$search_type] as $key => $value )
		{
			if ($value == 'time' && $req['get'][$key] != '')
			{
				$req['get'][$key] = $tk->f->date2int($req['get'][$key]);
			}
			switch ($key)
			{
				
				case 'starttime' :
					$req['get'][$key] = ($req['get'][$key] != '') ? $req['get'][$key] : $tk->f->date2int(date('d/m/Y', $tk->config['time']));
					$where_condition[] = "time >= '{$req['get'][$key]}'";
				break;
				case 'endtime' :
					$req['get'][$key] = ($req['get'][$key] != '') ? $req['get'][$key] : $tk->config['time'];
					$where_condition[] = "time <= '{$req['get'][$key]}'";
				break;
				case 'ip' :
					$where_condition[] = "$key = '" . $tk->f->encode_ip($req['get'][$key]) . "'";
				break;
				default :
					$where_condition[] = "$key = '{$req['get'][$key]}'";
				break;
			
			}
		}
		$where_condition = implode(" AND ", $where_condition);
		$order_by = "time asc";
		
		$sql = "SELECT SQL_CACHE id,$item_fields from $item_table where $where_condition order by $order_by";
		
		if (! ($result = $tk->db->sql_query($sql)))
		{
			$error = 'sql';
			goto end;
		}
		$i = 0;
		while($row = $tk->db->sql_fetchrow($result))
		{
			$data_json['p' . $i]['user'] = $tk->_page->pv['users'][$row['user_id']];
			$data_json['p' . $i]['time'] = date('d/m/Y H:i:s', $row['time']);
			$data_json['p' . $i]['ip'] = $tk->f->decode_ip($row['ip']);
			$i++;
		}
		$tk->page->_pv['json'] = 1;
		$tk->page->_pv['data_json'] = preg_replace('%\[(.+)\]%', '$1', json_encode($data_json));
	
	}
	
	/* default ! do not edit */
	$tk->page->_pv['items'] = $item_array;
	$tk->page->_pv['search'] = $search_array;
	$tk->page->_pv['data']['act'] = $req['get']['act'];
	$tk->page->_pv['data'][$req['get']['act']] = $req['language'] . '-' . $pagename . '.html?';
}

if ($req['get']['act'] == 'getitem')
{
	$id = (int) $req['get']['id'];
	
	$sql = "SELECT SQL_CACHE id,$item_fields from $item_table where id = '$id'";
	
	$result = $tk->db->sql_query($sql);
	
	$data_json = $tk->db->sql_fetchrow($result);
	
	$tk->page->_pv['json'] = 1;
	$tk->page->_pv['data_json'] = preg_replace('%\[(.+)\]%', '$1', json_encode($data_json));

}

end: